/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package id2212.homework2;

import bankrmi.Account;
import bankrmi.Bank;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.logging.Level;
import java.util.logging.Logger;
import marketrmi.MarketInter;

/**
 *
 * @author Guillaume, Yann
 */
public class Client extends UnicastRemoteObject implements ClientInter {

    static final String USAGE = "java bankrmi.Client <bank_url>";
    Account account;
    Bank bankObj;
    public MarketInter marketObj;
    String bankname = "rmi://localhost/suedenBank";
    String marketName = "rmi://localhost/kistaMarket";
    String clientname;
    Client_ui clUI;
    MarketResume marketResume;

    public static void main(String[] args) {
        try {
            new Client(args);
        } catch (RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public Client(String[] args) throws RemoteException {
        try {
            bankObj = (Bank) Naming.lookup(bankname);
            marketObj = (MarketInter) Naming.lookup(marketName);
        } catch (Exception se) {
            System.out.println("The runtime failed: " + se);
            System.exit(0);
        }
        System.out.println("The client is connected to bank: " + bankname);
        System.out.println("The client is connected to market: " + marketName);
        clUI = new Client_ui(this);
    }

    public void changeClientUiNameAndAmount() {
        try {
            clUI.setAmountCount(account.balance());
        } catch (RemoteException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("unable to retrieve the account balance");
        }
        clUI.setNameAccount(clientname);
        clUI.enableMarket();
    }

    @Override
    public void createNewAccount(String name, String password, float deposit) throws RemoteException, bankrmi.Rejected {
        clientname = name;
        bankObj.newAccount(name, password, deposit);
        account = bankObj.getAccount(name, password);
        changeClientUiNameAndAmount();
    }

    @Override
    public void getAccount(String name, String password) throws RemoteException, bankrmi.Rejected {
        System.out.println("try to get your account with the name " + name);
        clientname = name;
        account = bankObj.getAccount(name, password);
        changeClientUiNameAndAmount();
    }

    @Override
    public void callBackWish(String str) {
        marketResume.setNotifTtext(str);
        marketResume.refresh();
        clUI.setInformModifAccount(str);
        changeClientUiNameAndAmount();
    }

    @Override
    public void callBackGotWish(String str) {
        marketResume.setNotifTtext(str);
        marketResume.refresh();
        clUI.setInformModifAccount(str);
        changeClientUiNameAndAmount();
    }

    @Override
    public Account getClientAccount() throws RemoteException {
        return account;
    }
}
